package com.tuteng.administrator.ashospital;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.ShapeBadgeItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.tuteng.administrator.ashospital.application.hospitalAppication;
import com.tuteng.administrator.ashospital.beans.usertest;



import java.util.Map;

import cn.jpush.android.api.JPushInterface;

import static android.webkit.ConsoleMessage.MessageLevel.LOG;

public class MainActivity extends AppCompatActivity {

    private OneFragment mOneFragment;
    private TwoFragment mTwoFragment;
    private ThreeFragment mThreeFragment;
  //  private FourFragment mfourFragment;
    private Fragment[] mFragments;
    private BottomNavigationBar mBottomNavigationBar;
    private TextBadgeItem mTextBadgeItem;
    private ShapeBadgeItem mShapeBadgeItem;
    private int index;//点击的fragment的下标
    private int currentTabIndex=0;//当前的fragment的下标
    private int number=3;//显示的数字
    private hospitalAppication appication;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test_q);
        initView();//实例化View
        initBottomNavigationBar();//BottomNavigationBar控制底部导航栏的实现
        initListener();//点击事件
    }

    private void initListener() {
/**
        findViewById(R.id.bt_add).setOnClickListener(new View.OnClickListener() {//数字++事件
            @Override
            public void onClick(View v) {
                if (number<0){
                    mTextBadgeItem.hide();
                    number=1;
                }else {
                    number++;
                    mTextBadgeItem.setText(number+"").show();
                }
            }
        });
        findViewById(R.id.bt_cut).setOnClickListener(new View.OnClickListener() {//数字--事件
            @Override
            public void onClick(View v) {
                if (number<0){
                    mTextBadgeItem.hide();
                    number=0;
                }else {
                    mTextBadgeItem.setText(number+"").show();
                    number--;
                }
            }
        });
**/
        OkGo.getInstance().init(getApplication());
usertest us=new usertest();
us.setId("3");
us.setAge("26");
us.setName("黄启位");
  final String js= JSON.toJSONString(us);
        mBottomNavigationBar //设置lab点击事件
                .setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(int position) {
                        switch (position){
                            case 0:
                                index = 0;
                                OkGo.<usertest>post("http://192.168.1.102:7789/ceshi")
                                        .tag(this)
                                        .upJson(js)
                                   .execute(new JsonCallback<usertest>() {
                                       @Override
                                       public void onSuccess(Response<usertest> response) {
                                           usertest u=response.body();
                                         System.out.print(u.getAge()+"------->"+u.getName());
                                       }
                                   });
                                break;
                            case 1:
                                 index = 1;
                                break;
                            case 2:
                          index=2;
 System.out.print("------------->"+index);
                                break;
                                default:break;
                          /**  case 3:
                                index = 3;
                                break; **/
                        }
                     /**   if (index == 2) {
                            startActivity(new Intent(MainActivity.this, ChatActivity.class));
                            return;
                        }
                        **/
                        if (currentTabIndex != index) {

                            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
                            trx.hide(mFragments[currentTabIndex]);
                            if (!mFragments[index].isAdded()) {
                                trx.add(R.id.fl, mFragments[index]);
                            }
                            trx.show(mFragments[index]).commit();
                        }
                        currentTabIndex = index;
//                        Toast.makeText(MainActivity.this,"onTabSelected"+position,Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onTabUnselected(int position) {
                     /**
                        if (index==2){
                            mBottomNavigationBar.selectTab(position);
                        } **/
//                        Toast.makeText(MainActivity.this,"onTabUnselected"+position,Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onTabReselected(int position) {
//                        Toast.makeText(MainActivity.this,"onTabReselected"+position,Toast.LENGTH_SHORT).show();

                    }
                });
    }

    private void initBottomNavigationBar() {

       mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);

        mBottomNavigationBar //值得一提，模式跟背景的设置都要在添加tab前面，不然不会有效果。
                .setActiveColor(R.color.green);//选中颜色 图标和文字
//                .setInActiveColor("#8e8e8e")//默认未选择颜色
//                .setBarBackgroundColor(R.color.white);//默认背景色

        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.shouye_selected,"首页")
                        .setInactiveIcon(ContextCompat.getDrawable(MainActivity.this,R.mipmap.shouye))
                        .setBadgeItem(mShapeBadgeItem))
                .addItem(new BottomNavigationItem(R.mipmap.yiyuandaohang_selected,"医院导航")
                        .setInactiveIcon(ContextCompat.getDrawable(MainActivity.this,R.mipmap.yiyuandaohang)))
                .addItem(new BottomNavigationItem(R.mipmap.wode_selected,"我的")
                        .setInactiveIcon(ContextCompat.getDrawable(MainActivity.this,R.drawable.wode))
                        .setBadgeItem(mTextBadgeItem))
                .setFirstSelectedPosition(0)//设置默认选择的按钮
                .initialise();//所有的设置需在调用该方法前完成
    }

    private void initView() {
      appication=hospitalAppication.getInstance();
     String rid = JPushInterface.getRegistrationID(getApplicationContext());
        Log.e("---->",rid);
        mOneFragment=new OneFragment();
        mTwoFragment=new TwoFragment();
        mThreeFragment=new ThreeFragment();
       // mfourFragment =new FourFragment();
        mBottomNavigationBar= (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);

      /**  mTextBadgeItem = new TextBadgeItem()
                .setBorderWidth(4)
                .setBackgroundColorResource(R.color.colorAccent)
                .setAnimationDuration(200)
                .setText("3")
                .setHideOnSelect(false);

        mShapeBadgeItem = new ShapeBadgeItem()
                .setShapeColorResource(R.color.colorPrimary)
                .setGravity(Gravity.TOP | Gravity.END)
                .setHideOnSelect(false);**/

        mFragments=new Fragment[]{mOneFragment,mTwoFragment,mThreeFragment};
        FragmentTransaction trx1 = getSupportFragmentManager().beginTransaction();
        if (!mFragments[index].isAdded()) {
            trx1.add(R.id.fl, mFragments[index]);
        }
        trx1.show(mFragments[index]).commit();
    }
}
